package
{
	import flash.display.Bitmap;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import net.hires.utils.Stats;
	
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	
	import org.papervision3d.Papervision3D;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.core.controller.AnimationController;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.materials.shaders.FlatShader;
	import org.papervision3d.materials.shaders.GouraudShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.view.BasicView;
	
	[SWF (width="800", height="600", backgroundColor="#cccccc", frameRate="60")]
	
	public class Main extends BasicView
	{
		public static const COLLADA_BASIC_SAMPLES :String = "assets/Documents/Public Section/COLLADA 1.4.1 Basic Samples";
		
		[Embed (source="/assets/che.jpg")] private var TestTexture:Class;
		
		public var pointLight :PointLight3D;
		public var dae :DAE;
		
		public function Main()
		{
			Papervision3D.useRIGHTHANDED = true;
			
			super(0, 0, true, false, CameraType.FREE);
			
			stage.quality = StageQuality.LOW;
			
			pointLight = new PointLight3D(true);
			scene.addChild(pointLight);
			pointLight.x = 4000;
			pointLight.y = 3000;
			pointLight.z = -7000;
			
			var materials :MaterialsList = new MaterialsList();
		
			var material :MaterialObject3D = new WireframeMaterial();
			material.doubleSided = true;
			
			var shader :FlatShader = new FlatShader(pointLight, 0xaaaaaa, 0);
			var bm :Bitmap = new TestTexture();
			var shadedMaterial :ShadedMaterial = new ShadedMaterial(new BitmapMaterial(bm.bitmapData), shader);
			shadedMaterial.doubleSided = true;
		
			material = new FlatShadeMaterial(pointLight);
			material.doubleSided = true;
			
		//	materials.addMaterial(material, "Material__128");
			materials.addMaterial(new BitmapMaterial(bm.bitmapData), "Material__128");
			
			var dae :DAE = new DAE("house");
			dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, onColladaComplete);
			//dae.load("assets/Shelby_GT500_c4d.dae");
		//	dae.load("assets/mustang_bullit.dae", materials);
		//	dae.load("assets/mustang_small.dae");
		//	dae.load("assets/girl2b.dae");
		//	dae.load("assets/monster.dae");
		//	dae.load("assets/page_02.dae", materials);
		//	dae.load("assets/airplane_feb28_foldv02.dae");
		//	dae.load(COLLADA_BASIC_SAMPLES + "/Seymour/Seymour.dae");
		//	dae.load(COLLADA_BASIC_SAMPLES + "/Seymour_Plane/seymourplane.dae");
			//dae.load("assets/raices03.dae");
		//	dae.load("assets/CubeAndRobot/robot.dae");
		//	dae.load("assets/clips/planeAnimation2.dae");
		//	dae.load("assets/pug2.dae");
		//	dae.load("assets/morph/diego verticesmorp.dae");
			dae.load("assets/morph/diego morphbake.dae");
			
			var s :Stats = new Stats();
			addChild(s);
			
		//	camera.z = 100;
			camera.y = 400;
		//	camera.x = 50;
		//	camera.z = 1000;
			camera.lookAt(DisplayObject3D.ZERO);
			
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		protected override function onRenderTick(event:Event=null):void
		{
			if(dae)
			{
				//dae.rotationY++;
				/*
				var time :int = getTimer();
				var elapsed :Number = (time - _currentTime) * 0.001;
				
				if(elapsed > (_endTime-_startTime))
				{
					_currentTime = time;
					elapsed = 0;
				}
				
				for each(var track :AbstractTrack in dae.channels)
				{
					track.tick(elapsed);
				}
				*/
			}
			super.onRenderTick(event);
		}
		
		private function onColladaComplete(e:FileLoadEvent):void
		{
			this.dae = e.target as DAE;

			dae.scale = 5;
			//dae.rotationX = -90;
			dae.rotationY = 180;

			if(dae.animation)
			{
				trace("start playing");
				dae.animation.start();
			}
			
			scene.addChild(this.dae);
			
			camera.lookAt(this.dae);
			
			startRendering();
			//singleRender();	
			

		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			if(dae && dae.animation)
			{
				if(dae.animation.state == AnimationController.STATE_PLAYING)
				{
					dae.animation.pause();
				}
				else
				{
					dae.animation.resume();	
				}
			}
		}
		
		private function testGoogle():void
		{
			var url :String = "http://sketchup.google.com/3dwarehouse/download?mid=af796bbeb2990931a1ce49849c98d31c&rtyp=zip&fn=modern+sofa&ctyp=other&ts=1208288257000";
			
			var loader :URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onLoadComplete);	
			loader.load(new URLRequest(url));
		}
		
		private function onLoadComplete(e:Event):void
		{
			var loader :URLLoader = e.target as URLLoader;
			var zipFile :ZipFile = new ZipFile(loader.data);
			for(var i:int = 0; i < zipFile.entries.length; i++) {
			    var entry:ZipEntry = zipFile.entries[i];
			    trace(entry.name + " " + entry.size);
			}
		}
	}
}